<template>
	<view>
		<!-- 我的收货地址 -->
		<u-modal :asyncClose="true" :show="addressShow" :showCancelButton="true" @confirm="confirm"
			@cancel="addressShow=false" cancelColor="#333333" cancelText="关闭" confirmColor="#E86C64" confirmText="保存"
			ref="uModal" style="padding: 0;">
			<view class="address-box">
				<view class="address-title">收货地址</view>
				<view class="address-from">
					<u-form :labelWidth="100" :model="addressParams" :rules="rules" labelPosition="left" ref="form1">
						<u-form-item :required="true" borderBottom label="联系人" prop="receiveName" ref="item1">
							<u-input border="none" v-model="addressParams.receiveName"></u-input>
						</u-form-item>
						<u-form-item :required="true" borderBottom label="联系电话" prop="receivePhone">
							<u-input border="none" v-model="addressParams.receivePhone"></u-input>
						</u-form-item>
						<u-form-item :required="true" label="收货地址" prop="receiveAdress" ref="item12">
							<u-input border="none" v-model="addressParams.receiveAdress"></u-input>
						</u-form-item>
					</u-form>
				</view>
			</view>
		</u-modal>
		<!-- 我的收货地址 -->
	</view>
</template>

<script>
	export default {
		name: 'address',
		data() {
			return {
				addressShow: false,
				firstAdd: false,
				addressParams: {
					address: "",
					receiveAdress: "",
					receiveName: "",
					receivePhone: "",
					label: "",
				},
				rules: {
					"receiveName": {
						type: "string",
						required: true,
						message: "请填写联系人",
						trigger: ["blur", "change"],
					},
					"receivePhone": {
						type: "number",
						required: true,
						message: "请填写联系电话",
						trigger: ["blur", "change"],
					},
					"receiveAdress": {
						type: "string",
						required: true,
						message: "请填写收货地址",
						trigger: ["blur", "change"],
					},
				},
			}
		},
		methods: {
			showAddress() {
				this.$http
					.get("receive/getCurrentUserReceive", {})
					.then((res) => {
						console.log(res);
						if (res) {
							this.addressParams = res;
							this.firstAdd = false;
						} else {
							this.firstAdd = true;
						}
						this.addressShow = true;
					})
					.catch((err) => {
						this.firstAdd = true;
						this.addressShow = true;
					});
			},
			confirm() {
				this.$refs.form1.validate().then(res => {
					this.addressParams.address = this.addressParams.receiveAdress
					// this.addressParams.buyerUserId = 
					this.$http.post(this.firstAdd ? "receive/save" : "receive/update", this.addressParams).then(
						(res) => {
							if (res) {
								this.firstAdd = false;
								this.addressShow = false;
								uni.showToast({
									title: "操作成功",
								});
								this.$emit("update")
							}
						});
				}).catch(errors => {
					console.log(errors);
					uni.$u.toast('请检查数据是否完整')
				})
			},
		},
	}
</script>

<style lang="scss" scoped>

</style>
